let server = require("express")();
let mysql = require("mysql");
let expressStatic = require("express-static");
let port = 8080;

server.all("*",function(req,res,next){
    //设置允许跨域的域名，*代表允许任意域名跨域
    res.header("Access-Control-Allow-Origin","*");
    //允许的header类型
    res.header("Access-Control-Allow-Headers","content-type");
    //跨域允许的请求方式 
    res.header("Access-Control-Allow-Methods","DELETE,PUT,POST,GET,OPTIONS");
    if (req.method.toLowerCase() == 'options')
        res.send(200);  //让options尝试请求快速结束
    else
        next();
})

let sql = mysql.createConnection({
    host:"localhost",
    user:"root",
    password:"123456",
    port:3307,
    database:"adidas"
})
sql.connect();

server.get("/resiger",(request,response)=>{
	let username = request.query.username;
	let password = request.query.password;
	
	sql.query(`SELECT * FROM user WHERE username="${username}"`,(error,data)=>{
		if(error){
			console.log(error)
			response.end("error")
			return
		}
		if(data.length === 0){
			sql.query(`INSERT INTO user (username,password) VALUES ("${username}","${password}")`,(error,result)=>{
				if(error){
					console.log(error);
					response.end("error");
					return
				}
				response.end("success")
			})
		}
		else{
			response.end("exist")
		}
	})
})

server.get("/login",function(request,response){
    let username = request.query.username;
    let password = request.query.password;
    sql.query(`SELECT * FROM user WHERE username="${username}"`,(error,data)=>{
		if(data.length === 0){
			response.end("error")
			return;
		}

		if(data[0].password === password){
			response.end("success")
		}
		else{
			response.end("error")
		}
	})
})

server.get("/listItem",(request,response)=>{
	sql.query("select * from list",(error,data)=>{
		if(error){
			console.log(error);
			response.end("error");
			return;
		}
		response.end(JSON.stringify(data));
	})
})

server.get("/information",(request,response)=>{
	let id = request.query.id;
	sql.query(`select * from list where id=${id}`,(error,data)=>{
		if(error){
			console.log(error);
			response.end("error");
			return;
		}
		response.end(JSON.stringify(data));
	})
})


server.get("/selectCar",(request,response)=>{
	sql.query("select * from car",(error,data)=>{
		if(error){
			console.log(error);
			response.end("error");
			return;
		}
		response.end(JSON.stringify(data));
	})
})

server.get("/selectUserCar",(request,response)=>{
	let username = request.query.username;
	sql.query(`select * from car where username="${username}"`,(error,data)=>{
		if(error){
			console.log(error);
			response.end("error");
			return;
		}
		response.end(JSON.stringify(data));
	})
})

server.get("/insertCar",(request,response)=>{
	let username = request.query.username;
	let title = request.query.title;
	let color = request.query.color;
	let size = request.query.size;
	let listId = request.query.listId;
	let price = request.query.price;
	let src = request.query.src;
	sql.query(`insert into car (username,title,color,size,listId,price,src) values ("${username}","${title}","${color}","${size}",${listId},"${price}","${src}")`,(error,data)=>{
		if (error) {
			console.log(error);
			response.end("error");
			return;
		}
		response.end("success");
	})
})

server.get("/deleteItem",(request,response)=>{
	let id = request.query.id;
	sql.query(`delete from car where id=${id}`,(error,data)=>{
		if (error) {
			console.log(error);
			response.end("error");
			return;
		}
		response.end("success");
	})
})

server.get("/orderItem",(request,response)=>{
	let username = request.query.username;
	let listId = request.query.listId;
	let name = request.query.name;
	let phone = request.query.phone;
	let address = request.query.address;
	let code = request.query.code;
	let title = request.query.title;
	let color = request.query.color;
	let size = request.query.size;
	let price = request.query.price;
	let state = request.query.state;
	let img = request.query.img;
	let data = request.query.data;
	sql.query(`INSERT INTO orderKu (username,listId,nameme,phone,address,codeme,title,color,size,price,state,img,data) VALUES ("${username}",${listId},"${name}","${phone}","${address}","${code}","${title}","${color}","${size}","${price}","${state}","${img}","${data}")`,(error,data)=>{
		if(error){
			console.log(error);
			response.end("error");
			return;
		}
		response.end("success");
	})
})

server.get("/selectOrderKu",(request,response)=>{
	let username = request.query.username;
	sql.query(`select * from orderKu where username="${username}"`,(error,data)=>{
		if(error){
			console.log(error);
			response.end("error");
			return;
		}
		response.end(JSON.stringify(data));
	})
})

server.get("/removeOrder",(request,response)=>{
	let id = request.query.id;
	sql.query(`delete from orderKu where id=${id}`,(error,data)=>{
		if(error){
			console.log(error);
			response.end("error");
			return;
		}
		response.end(JSON.stringify(data));
	})
})

server.use(expressStatic(__dirname+`/static`))

server.listen(port);
console.log(`server is running at ${port}`)